package com.itxx.udp.Unicast.udpDemo2;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class RecevieDemo1 {
    public static void main(String[] args) throws IOException {
        //1.创建接受端对象  DatagramSocket(int port)  从哪个端口接受数据
        DatagramSocket ds = new DatagramSocket(10000);

        //2.创建新的报包(箱子)
        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
        //3.接受数据,把数据方进新的DatagramPacket报包(箱子)
        ds.receive(dp);
        //4.从DatagramPacket报包拿数据
        byte[] data = dp.getData();
        //获取本次实际获取的数据长度
        int length = dp.getLength();
        //将byte数组解码为String
        System.out.println(new java.lang.String(data, 0, length));

        //释放资源

    }
}
